<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: my.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
class my extends admin{	
	function __construct() {
		parent::__construct();
		$this->main();
	}

	function main(){
		if(isset($_GET['ajax']) && !empty($_POST)) {
			$func = 'set_'.$_GET['ajax'];
			if($_GET['ajax']=='password') {
				$this->$func();
			}
		}elseif(isset($_GET['action'])) {
			if($_GET['action'] == 'index') {
				include load_tpl(__CLASS__);
			}elseif($_GET['action'] == 'newtab'){
				include load_tpl('form');
			}elseif(in_array($_GET['action'], array('password'))) {
				$this->form = ADMNAME.'?mod=my&ajax='.$_GET['action'];
				include load_tpl('my_password');
			}
		}
	}

	function set_password() {
		$_POST = _trim($_POST);
		$err = 1;
		$name = '';
		if(empty($_POST['oldpw'])) {
			$name = 'oldpw';
			$msg = '原密码不能为空';
		}elseif(empty($_POST['newpw'])) {
			$name = 'newpw';
			$msg = '新密码不能为空';
		}elseif(strlen($_POST['newpw']) < 6) {
			$name = 'newpw';
			$msg = '新密码不能小于6位数';
		}elseif($_POST['newpw'] == $_POST['oldpw']) {
			$name = 'newpw';
			$msg = '新密码不能和旧密码相同';
		}elseif($_POST['newpw'] != $_POST['confirm_pw']) {
			$name = 'confirm_pw';
			$msg = '确认密码与新密码不相同';
		}elseif(md5($_POST['oldpw']) != $this->auser['password']) {
			$name = 'oldpw';
			$msg = '原密码不正确';
		}else{
			$password = md5($_POST['newpw']);
			$ret = $this->db->query('UPDATE `'.TW_DBPRE.'admin` SET `password`='.S($password).' WHERE `uid`='.$this->auser['uid']);
			if(!$ret || !$this->db->query('UPDATE `'.TW_DBPRE.'member` SET `password`='.S($password).' WHERE `uid`='.$this->auser['uid'])) {
				$msg = '写入数据库出错';
			}else{
				$err = 0;
				$msg = '修改完成';
			}
		}
		exit('{"err":'.$err.', "name":"'.$name.'", "msg":"'.$msg.'"}');
	}

	function display(){
		if($_GET['action'] == 'newtab') {
			$this->{'tr_'.$_GET['action']}();
		}elseif($_GET['action'] == 'password') {
			echo '<div class="tb_t">'.$this->title.'</div>';
			echo '<table class="tb">';
			$this->{'tr_'.$_GET['action']}();
			echo '</table>';
			echo '<div class="tb_b"><input type="submit" value="修改" class="but1" /></div>';
		}
	}

	function tr_newtab(){
		echo '<div class="warning bnote"><i></i><b>感谢选用TwCms！欢迎 <a href="http://bbs.twcms.cn/forum.php?mod=forumdisplay&fid=43" target="_blank">反馈BUG</a>，<a href="http://bbs.twcms.cn/forum.php?mod=forumdisplay&fid=44" target="_blank">反馈建议</a>！</b></div></div>';
	}

	function tr_password(){
		load_tr('原密码', '', 'password', 'oldpw');
		load_tr('新密码', '', 'password', 'newpw');
		load_tr('确认密码', '', 'password', 'confirm_pw');
	}

	function get_user_info() {
		return $this->db->fetch_first('SELECT * FROM `'.TW_DBPRE.'member` WHERE uid='.$this->auser['uid'].' LIMIT 1'); 
	}

	function get_other() {
		$str = '';
		$str .= ini_get('safe_mode') ? 'safe_on ' : 'safe_off ';
		if(extension_loaded('gd')) {
			function_exists('imagepng') && $str .= 'png ';
			function_exists('imagejpeg') && $str .= 'jpg ';
			function_exists('imagegif') && $str .= 'gif ';
		}
		extension_loaded('iconv') && $str .= 'iconv ';
		extension_loaded('mbstring') && $str .= 'mbstring ';
		extension_loaded('zlib') && $str .= 'zlib ';
		extension_loaded('ftp') && $str .= 'ftp ';
		function_exists('fsockopen') && $str .= 'socket';
		return $str;
	}

	function get_new_art() {
		return $this->db->fetch_all('SELECT id,title,time FROM `'.TW_DBPRE.'article` ORDER BY `id` DESC LIMIT 8');
	}
}